import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from mynewsweb.lib.base import BaseController, render
from mynewsweb.lib import helpers as h

from mynewsweb.model.meta import Session
from mynewsweb.model import News, KernelResults, StopWords
from sqlalchemy.sql.expression import desc
from sqlalchemy.orm import join
from pylons.decorators.cache import beaker_cache

from NewsMining.crawler.preprocessText import *

log = logging.getLogger(__name__)

class NewsController(BaseController):
    @beaker_cache(expire=600, type='memory', query_args=True)
    def list(self):
        c.start = int(request.params.get('start'))
        c.end = int(request.params.get('end'))
        c.size = int(request.params.get('size'))
        c.class_id = int(request.params.get('class_id'))

        c.news = Session.query(News).filter(News.class_id == c.class_id).\
                                     order_by(desc(News.reads))[c.start:c.end]
        # WebFlash is used to flash messages to the user. Flashed messages
        #   will survive redirects and will only be displayed once.
        #h.flash_alert("Podatke je se potrebno osveziti :)")
        #h.flash_info("Podatke je se potrebno osveziti :)")
        h.flash_ok("Podatki so osvezeni 1. marca. 2010")
        return render('/news/news-list.mako')

    @beaker_cache(expire=600, type='memory', query_args=True)
    def news(self):
        c.news_id = request.params.get('news_id')
        c.news = Session.query(News).filter_by(news_id=int(c.news_id)).one()

        c.news_data = dict([(n.news_id, (n.title, n.class_id, n.reads)) for n in Session.query(News.news_id, News.title, News.class_id, News.reads).all()])

        kernel_results = Session.query(KernelResults).\
                            filter(KernelResults.news_id_1 == c.news_id).\
                            order_by(desc(KernelResults.similarity))

        c.kernel_results_0 = kernel_results.filter(KernelResults.kernel_id == 0)[:8]
        c.kernel_results_1 = kernel_results.filter(KernelResults.kernel_id == 1)[:8]
        c.kernel_results_2 = kernel_results.filter(KernelResults.kernel_id == 2)[:8]

        return render('/news/news.mako')

    @beaker_cache(expire=600, type='memory', query_args=True)
    def top_similarity(self):
        c.start = int(request.params.get('start'))
        c.end = int(request.params.get('end'))
        c.size = int(request.params.get('size'))
        #c.class_id = int(request.params.get('class_id'))
        c.kernel_id = request.params.get('kernel_id')

        c.top = Session.query(KernelResults).order_by(desc(KernelResults.similarity)).\
                            filter(KernelResults.kernel_id == c.kernel_id)[c.start:c.end]
        return render('/news/news-top-similarity.mako')

    def pics(self):
        results_id = int(request.params.get('results_id'))
        if results_id == 0:
            return render('/news/pics.mako')
        elif results_id == 1:
            return render('/news/SVM-Ngrams-size-2500.mako')
        elif results_id == 2:
            return render('/news/SVM-Ngrams-size-2500-bigspace.mako')